package com.online.controller;

import com.online.entity.*;
import com.online.service.LoginService;
import com.online.service.impl.LoginServiceImpl;
import com.online.util.ApiResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户登录
 */
@RestController
@Slf4j
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public ApiResult login(@RequestBody Login login){
        Integer username = login.getUsername();
        String password = login.getPassword();
        Admin adminRes = loginService.adminLogin(username, password);
        if (adminRes != null) {
            log.info("超级管理员登录成功");
            return ApiResultHandler.buildApiResult(200, "请求成功", adminRes);

        }

        Teacher teacherRes = loginService.teacherLogin(username,password);
        if (teacherRes != null) {
            log.info("老师登录成功");
            return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
        }

        Student studentRes = loginService.studentLogin(username,password);
        if (studentRes != null) {
            log.info("学生登录成功");
            return ApiResultHandler.buildApiResult(200, "请求成功", studentRes);
        }

        return ApiResultHandler.buildApiResult(400, "请求失败", null);
    }
}
